var config = require('../../../../config/config.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    aid: 1,
    sid: 1,
    id: 0,
    shop_name: '',
    totalPrice: 0,
    orderDisplay: true,
    status:0,
    item: {
      num: "1234567890",
      date: "2018-1-10"
    },
    test: {
      img: "/image/home/place.png",
      name: "小丸子",
      tel: "13622194832",
      address: "武汉市 洪山区东湖高新技术开发区xxxxx"
    },
    orderData: []
  },
  //随便逛逛
  buttonTo: function (event) {
    wx.navigateBack({
      delta:-1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getOrerDetailUrl: function (e) {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.order_detail,
      data: {
        id: this.data.id,
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          order_list: res.data.data,
        })
      },
      fail: function () {
      },
    })
  },
  //删除订单
  cancal_pay: function (e) {
    var member_info = (wx.getStorageSync('member_info'));
    var that = this;
    wx.request({
      url: config.order_delete,
      data: {
        id: this.data.id,
        aid: this.data.aid,
        mid: member_info.id,
        sid: this.data.sid,

      },
      method: 'GET',
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          duration: 2000
        });
        that.setData({
          orderDisplay: false
        })
        // wx.navigateBack({
        //   delta: -1
        // })
      },
      fail: function () {
      },
    })
  },
  //去支付
  to_pay: function (e) {
    var member_info = (wx.getStorageSync('member_info'));
    var that = this;
    wx.request({
      url: config.order_pay,
      method: "POST",
      data: {
        aid: this.data.aid,
        mid: member_info.id,
        sid: this.data.sid,
        order_id: e.currentTarget.dataset.id,
        openid: member_info.openid
      },
      success: function (res) {
        if (!res.data.status) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        } else {
          that.Payment(res.data.data)
        }
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  //微信支付
  Payment: function (data) {
    var ispaysuccess = 0;
    wx.requestPayment({
      'timeStamp': String(data.timeStamp),
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function (res) {
        ispaysuccess = 1;
        wx.redirectTo({
          url: '../pay-success/pay-success?ispaysuccess=' + ispaysuccess,
        })
      },
      'fail': function (res) {
        // wx.showToast({ title: '支付失败', icon: 'loading', duration: 20000 });
        wx.redirectTo({
          url: '../pay-success/pay-success?ispaysuccess=' + ispaysuccess,
        })
      }
    })
  },
  onLoad: function (options) {
    this.setData({
      shop_name: wx.getStorageSync('shop_name'),
      id: options.id,
      status: options.status
    })
    this.getOrerDetailUrl()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  backTo: function (e) {
    wx.switchTab({
      url: '../../../shopCity/shopCity',
    })
  },
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})